package com.example.demo.controller;

import entity.Plan;
import Mapper.PlanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class PlanController {

    @Autowired
    private PlanMapper planMapper;

    @PostMapping("/insertplan")
    public int insertPlan(@RequestBody Plan plan) {
        // 检查是否已存在该计划
        Integer existingPlanId = planMapper.checkplan(plan.getType(), plan.getUser_id());
        if (existingPlanId != null) {
            return -1; // 如果计划已存在，返回 -1
        }
        // 插入新计划
        plan.setDays(0); // 初始天数设置为0
        int result = planMapper.insertplan(plan);
        return result;
    }

    @PostMapping("/updateplan")
    public int updatePlan(@RequestBody Plan plan) {
        // 更新计划天数
        int result = planMapper.updateplan(plan.getType(), plan.getUser_id());
        return result;
    }
}